package com.rickpan.repository;

import com.rickpan.entity.FileShare;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 文件分享Repository接口
 * 
 * @author RickPan Team
 * @since 2025-01-05
 */
@Repository
public interface FileShareRepository extends JpaRepository<FileShare, Long>, JpaSpecificationExecutor<FileShare> {

    /**
     * 根据分享码查找分享记录
     */
    Optional<FileShare> findByShareCode(String shareCode);

    /**
     * 根据分享码和状态查找分享记录
     */
    Optional<FileShare> findByShareCodeAndStatus(String shareCode, FileShare.ShareStatus status);

    /**
     * 根据文件ID查找分享记录
     */
    List<FileShare> findByFileId(Long fileId);

    /**
     * 根据用户ID查找分享记录
     */
    Page<FileShare> findByUserId(Long userId, Pageable pageable);

    /**
     * 根据用户ID查找所有分享记录（不分页）
     */
    List<FileShare> findByUserId(Long userId);

    /**
     * 统计用户分享数量
     */
    Long countByUserId(Long userId);

    /**
     * 根据用户ID和状态查找分享记录
     */
    Page<FileShare> findByUserIdAndStatus(Long userId, FileShare.ShareStatus status, Pageable pageable);

    /**
     * 根据用户ID和文件名搜索分享记录
     */
    @Query("SELECT fs FROM FileShare fs " +
           "JOIN FileInfo fi ON fs.fileId = fi.id " +
           "WHERE fs.userId = :userId " +
           "AND fi.originalName LIKE %:keyword%")
    Page<FileShare> findByUserIdAndFileNameContaining(@Param("userId") Long userId, 
                                                     @Param("keyword") String keyword, 
                                                     Pageable pageable);

    /**
     * 检查分享码是否已存在
     */
    boolean existsByShareCode(String shareCode);



    /**
     * 统计用户活跃分享数量
     */
    long countByUserIdAndStatus(Long userId, FileShare.ShareStatus status);

    /**
     * 查找已过期但状态仍为ACTIVE的分享记录
     */
    @Query("SELECT fs FROM FileShare fs " +
           "WHERE fs.status = 'ACTIVE' " +
           "AND fs.expireTime IS NOT NULL " +
           "AND fs.expireTime < :currentTime")
    List<FileShare> findExpiredActiveShares(@Param("currentTime") LocalDateTime currentTime);

    /**
     * 获取用户分享的总访问次数
     */
    @Query("SELECT COALESCE(SUM(fs.accessCount), 0) FROM FileShare fs WHERE fs.userId = :userId")
    Long getTotalAccessCountByUserId(@Param("userId") Long userId);

    /**
     * 获取今日访问次数
     */
    @Query("SELECT COALESCE(SUM(sal.id), 0) FROM ShareAccessLog sal " +
           "JOIN FileShare fs ON sal.shareId = fs.id " +
           "WHERE fs.userId = :userId " +
           "AND sal.accessTime >= :startTime " +
           "AND sal.accessTime < :endTime " +
           "AND sal.success = true")
    Long getTodayAccessCountByUserId(@Param("userId") Long userId, 
                                   @Param("startTime") LocalDateTime startTime,
                                   @Param("endTime") LocalDateTime endTime);

    /**
     * 删除用户的所有分享记录
     */
    void deleteByUserId(Long userId);

    /**
     * 删除文件的所有分享记录
     */
    void deleteByFileId(Long fileId);
}
